package com.heartware.sh.blackhole.retrofit;

import android.util.Log;

import com.google.gson.Gson;
import com.heartware.sh.blackhole.common.http.ParamPost;
import com.heartware.sh.blackhole.entity.BaseEntity;
import com.heartware.sh.blackhole.entity.Entity;
import com.heartware.sh.blackhole.entity.test.MyEntity;
import com.heartware.sh.blackhole.entity.UserEntitiy;
import com.heartware.sh.blackhole.entity.req.RegUserReq;
import com.heartware.sh.blackhole.ui.activity.MainActivity;
import com.heartware.sh.blackhole.view.ILoginView;
import com.heartware.sh.blackhole.view.IOneView;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okio.BufferedSink;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.adapter.rxjava.HttpException;
import rx.Observer;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/11/30 0030.
 */
public class NetWorkApi {

    public static final String TAG = NetWorkApi.class.getSimpleName();

    public static void getList(final MainActivity mActivity){

        BlackHoleService bhService = ServiceFactory.createServiceFrom(BlackHoleService.class, BlackHoleService.base_api);

        bhService.getList(0).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MyEntity>() {
                    @Override
                    public void onCompleted() {
                        Log.i(TAG, "onCompleted()");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG, "onError()");
                    }

                    @Override
                    public void onNext(MyEntity myEntity) {
                        Log.i(TAG, "onNext()");
                    }
                });

    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @param loginView
     */
    public static void doLogin(String username, String password, final ILoginView loginView){

        BlackHoleService bhService = ServiceFactory.createServiceFrom(BlackHoleService.class, BlackHoleService.base_api);

//        Map<String, String> params = new HashMap<String, String>();
//        params.put("username", username);
//        params.put("password", password);

        bhService.getUserInfo(username, password).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<UserEntitiy>() {
                    @Override
                    public void onCompleted() {
                        Log.i(TAG, "onCompleted()");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG, "onError()"+e);
//                        loginView.loginResult(false, "登录异常");

                        /**
                         * 向下转型, 转成具体的错误, 并且拿出错误信息
                         */
                        if(e instanceof HttpException){
                            ResponseBody body = ((HttpException) e).response().errorBody();
                            try {
                                String msg = body.string();
                                Log.e(TAG, msg);
                                UserEntitiy ue = new Gson().fromJson(msg, UserEntitiy.class);
                                loginView.loginResult(ue.isSuccess(), ue.getError());
//                                loginView.loginResult(false, msg);
                            } catch (IOException IOe) {
                                IOe.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onNext(UserEntitiy userEntitiy) {
                        Log.i(TAG, "onNext()");
                        loginView.loginResult(userEntitiy.isSuccess(), userEntitiy.getError());

                    }
                });

    }

    /**
     * 用户注册
     * @param username
     * @param password
     * @param loginView
     */
    public static void doReg(String username, String password, final ILoginView loginView){

        BlackHoleService bhService = ServiceFactory.createServiceFrom(BlackHoleService.class, BlackHoleService.base_api);

        RegUserReq rur = new RegUserReq(username, password);

        bhService.doReg(rur).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<UserEntitiy>() {
                    @Override
                    public void onCompleted() {
                        Log.i(TAG, "onCompleted()");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG, "onError()"+e);
//                        loginView.regResult(false, "注册异常");
                        /**
                         * 向下转型, 转成具体的错误, 并且拿出错误信息
                         */
                        if(e instanceof HttpException) {
                            ResponseBody body = ((HttpException) e).response().errorBody();
                            try {
                                String msg = body.string();
                                Log.e(TAG, msg);
                                UserEntitiy ue = new Gson().fromJson(msg, UserEntitiy.class);
                                loginView.regResult(ue.isSuccess(), ue.getError());
//                                loginView.loginResult(false, msg);
                            } catch (IOException IOe) {
                                IOe.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onNext(UserEntitiy userEntitiy) {
                        Log.i(TAG, "onNext()");
                        loginView.regResult(userEntitiy.isSuccess(), userEntitiy.getError());
                    }
                });

    }


}
